<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\Session;
use think\Db;
class Admin extends Controller
{
    public function admin()
    {
        return $this->fetch('admin');
    }

    public function loginadmin()
    {
        $data = input("post.");
        $date = Db::table('tb_administrator')->where('username', '=', $data['user'])->find();
        if ($date) {
            if ($date['password'] == md5($data['pwd'])) {
                Session::set('id', $date['id']);
                Session::set('loginuser', $date);
                echo json_encode(['code' => 200, 'msg' => '成功', 'user' => $date['username']]);
                die;
            } else {
                echo json_encode(['code' => 4003, 'msg' => '密码错误']);
                die;
            }
        } else {
            echo json_encode(['code' => 4003, 'msg' => '用户名错误或不存在']);
            die;
        }
    }
    public function show(){
        return $this->fetch('show');
    }

    public function listp()
    {
        $id = Session::get('id');
        if (empty($id)){
            echo json_encode(['code' => 4003, 'msg' => '请登录']);
            die;
        }
        $r_id = Db::table('u_r')->where('u_id', '=', $id)->value('r_id');
        $p_id = Db::table('r_p')->where('r_id', '=', $r_id)->column('p_id');
        $p_id=implode(",", $p_id);
        $permission=Db::table('permission')->where('id','in',$p_id)->select();
        $array = $this->getTree($permission);
//        var_dump($array);die;
//        foreach($array as $value){
//            $power[] = str_repeat('--', $value['level']).$value['name'].'<br />';
//        }
        if ($array){
            echo json_encode(['code' => 200, 'msg' => '成功','power'=>$array]);
            die;
        }else{
            echo json_encode(['code' => 4003, 'msg' => '没有权限']);
            die;
        }
    }
    function getTree($permission, $pid =0, $level = 0){
        static $list = [];
        foreach ($permission as $key => $value){
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid){
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = [
                    'id'=>$value['id'],
                    'pid'=>$value['pid'],
                    'level' =>$value['level'] ,
                    'description'=>$value['description'],
                    'name'=>str_repeat('&nbsp&nbsp&nbsp',$level*2).$value['name']
                ];
                //把这个节点从数组中移除,减少后续递归消耗
                unset($permission[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
               $this->getTree($permission, $value['id'], $level+1);

            }
        }
        return $list;
    }
    public function user(){
        return $this->fetch("user");
    }
    public function do_user(){
        $loginuser = Session::get('loginuser');
        if ($loginuser['id']!=1){
            echo json_encode(['code' => 4003, 'msg' => '没有权限']);
            die;
        }
        $date = Db::table('tb_administrator')->select();
        if ($date){
            echo json_encode(['code' => 200, 'msg' => '成功','date'=>$date]);
            die;
        }
    }
    public function add_power(){
        return $this->fetch("add_power");
    }
}